/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IsohuntResults.java
 *
 * Created on 3 Ιουν 2010, 12:53:44 μμ
 */
package tools.download.torrents.isohunt;

import help.HelpWindow;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import javax.swing.table.DefaultTableModel;
import myComponents.myGUI.MyDraggable;
import myComponents.myTableCellRenderers.IsoHuntInfoRenderer;
import myComponents.myTableCellRenderers.IsoHuntLinkRenderer;
import myComponents.myTableCellRenderers.IsoHuntSizeRenderer;
import tools.MySeriesLogger;
import tools.Skin;
import tools.download.torrents.AbstractTorrent;
import tools.download.torrents.TorrentConstants;

/**
 *
 * @author ssoldatos
 */
public class IsohuntResults extends MyDraggable implements TorrentConstants {

    private static final long serialVersionUID = 354646575474L;
    IsohuntTorrent selectedTorrent;
    private ArrayList<AbstractTorrent> torrents;

    /** Creates new form IsohuntResults
     * @param torrents
     */
    public IsohuntResults(ArrayList<AbstractTorrent> torrents) {
        MySeriesLogger.logger.log(Level.INFO, "Showing isohunt results form");
        this.torrents = torrents;
        MySeriesLogger.logger.log(Level.INFO, "Initializong components");
        initComponents();
        MySeriesLogger.logger.log(Level.FINE, "Components initialized");
        createTable();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel2 = new javax.swing.JPanel();
    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    table = new javax.swing.JTable();
    jLabel3 = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();
    bt_cancel = new myComponents.myGUI.buttons.MyButtonCancel();
    bt_help = new myComponents.myGUI.buttons.MyButtonHelp();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setUndecorated(true);

    jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    table.setAutoCreateRowSorter(true);
    table.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Title", "Files", "Size", "Seeds", "Leechers", "Download"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.String.class, java.lang.Integer.class, java.lang.Long.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class
      };
      boolean[] canEdit = new boolean [] {
        false, false, false, false, false, false
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }

      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
      }
    });
    table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_NEXT_COLUMN);
    table.setPreferredSize(new java.awt.Dimension(600, 300));
    table.setRequestFocusEnabled(false);
    table.setRowHeight(20);
    jScrollPane1.setViewportView(table);

    jLabel3.setText("Mouse over the title for more info about the torrent");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addGap(20, 20, 20)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 877, Short.MAX_VALUE)
          .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel3)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE)
        .addContainerGap())
    );

    jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+2));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Isohunt search results");

    bt_cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_cancelActionPerformed(evt);
      }
    });

    bt_help.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_helpActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 781, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bt_help, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bt_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(jPanel2Layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGap(10, 10, 10))))
    );
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
          .addComponent(jLabel1)
          .addComponent(bt_help, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(bt_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void bt_helpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_helpActionPerformed
      MySeriesLogger.logger.log(Level.INFO, "Showing help window");
      new HelpWindow(HelpWindow.DOWNLOAD_TORRENT);
  }//GEN-LAST:event_bt_helpActionPerformed

  private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
      dispose();
  }//GEN-LAST:event_bt_cancelActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private myComponents.myGUI.buttons.MyButtonCancel bt_cancel;
  private myComponents.myGUI.buttons.MyButtonHelp bt_help;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTable table;
  // End of variables declaration//GEN-END:variables

    /**
     * @return the selectedTorrent
     */
    public IsohuntTorrent getSelectedTorrent() {
        return selectedTorrent;
    }

    private void createTable() {
        MySeriesLogger.logger.log(Level.INFO, "Creating the results table");
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_TITLE).setPreferredWidth(340);
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_TITLE).setCellRenderer(new IsoHuntInfoRenderer());
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_FILES).setPreferredWidth(40);
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_LENGTH).setPreferredWidth(80);
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_LENGTH).setCellRenderer(new IsoHuntSizeRenderer());
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_SEEDS).setPreferredWidth(50);
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_LEECHERS).setPreferredWidth(50);
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_DOWNLOAD).setPreferredWidth(80);
        table.getColumnModel().getColumn(ISOHUNT_RESULTS_DOWNLOAD).setCellRenderer(new IsoHuntLinkRenderer());
        table.addMouseListener(new IsoHuntTableMouseListener());
        createModel();
    }

    private void createModel() {
        MySeriesLogger.logger.log(Level.INFO, "Populating the table with {0} torrents",torrents.size());
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        for (Iterator<AbstractTorrent> it = torrents.iterator(); it.hasNext();) {
            IsohuntTorrent torrent = (IsohuntTorrent) it.next();
            model.addRow(new Object[]{
                        torrent,
                        torrent.getFiles(),
                        torrent.getLength(),
                        torrent.getSeeds(),
                        torrent.getLeechers(),
                        torrent
                    });
        }
    }

    class IsoHuntTableMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1) {
                Point p = e.getPoint();
                int row = table.rowAtPoint(p);
                int col = table.columnAtPoint(p);
                if (col == ISOHUNT_RESULTS_DOWNLOAD) {

                    IsohuntTorrent torrent = (IsohuntTorrent) table.getValueAt(row, col);
                    MySeriesLogger.logger.log(Level.FINE, "Download torrent {0}",torrent.getLink());
                    IsohuntResults.this.selectedTorrent = torrent;
                    dispose();
                }
                super.mouseClicked(e);
            }

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1) {
                Point p = e.getPoint();
                int row = table.rowAtPoint(p);
                int col = table.columnAtPoint(p);
                if (col == ISOHUNT_RESULTS_DOWNLOAD) {
                    table.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }
                super.mouseClicked(e);
            }

        }
    }
}
